/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package crawler.model;

import business.service.MongoDbFactory;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

/**
 *
 * @author ngotandieu
 */
public class ActionModel {

    private static final String ACTION_COLLECTION = "action";
    private static final Logger logger = Logger.getLogger(CateModel.class);
    private static final Lock createLock_ = new ReentrantLock();
    private static ActionModel _instance = null;
    
    public static ActionModel getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new ActionModel();

                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }

    public ActionModel() {
        try {
        } catch (Exception ex) {
            logger.error("ActionModel: " + ex);
        }
    }
    
    public static void addAction(HttpServletRequest req, String user, String type, String action, String value) {
        try {
            BasicDBObject basicDBObject = new BasicDBObject("user", user)
                    .append("type", type)
                    .append("action", action)
                    .append("value", value)
                    .append("created", System.currentTimeMillis());
            
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(ACTION_COLLECTION);
            coll.insert(basicDBObject);
        } catch (Exception ex) {
            logger.error("CateModel.addCate: " + ex.getMessage(), ex);
        }
    }
}
